#include "./../../lvgl/lvgl.h"
#include "./../../lvgl/demos/lv_demos.h"
#include "./../../lv_drivers/display/fbdev.h"
#include "./../../lv_drivers/indev/evdev.h"
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>

#include "./../../inc/homePage.h"
#include "./../../inc/album.h"
#include "./../../inc/page_manager.h"

// static int GESTURE_CANCEL_THRESHOLD = 10;  // 手势取消阈值
static int COL_NUM           = 3;   // 固定3列
static int MARGIN            = 10;  // 边缘间距
static int GAP               = 10;  // 图片间距
static int IMG_WIDTH         = 220; // 图片宽度
static int IMG_HEIGHT        = 147; // 图片高度
static int img_index         = 0;
static int img_num           = 0;
static lv_obj_t * img_screen = NULL;
// static lv_obj_t * right_img = NULL;

file_node_p img_head = NULL;
static lv_point_t start_point;
static lv_point_t end_point;
static uint32_t start_time;

static void gesture_event_cb(lv_event_t * e);
static int get_gesture_direction(lv_point_t start, lv_point_t end, uint32_t time_diff);
static void show_big_img();
static void back_btn_event_cb(lv_event_t * e);
// static void change_img_left_event_cb(lv_event_t * e);
// static void change_img_left_event_cb(lv_event_t * e);

void album_init(void)
{
    img_head = file_node_init();
    // print_file_node(img_head);
    lv_obj_t * album_screen = lv_obj_create(NULL);
    lv_obj_set_style_bg_color(album_screen, lv_color_hex(BLACK), LV_PART_MAIN);
    lv_obj_set_style_bg_opa(album_screen, LV_OPA_COVER, LV_PART_MAIN);
    lv_obj_set_style_border_width(album_screen, 0, 0);
    lv_obj_set_style_radius(album_screen, 0, 0);
    lv_obj_set_style_pad_all(album_screen, 0, 0);

    lv_obj_t * img = lv_img_create(album_screen);
    lv_img_set_src(img, "S:res/img/car_bg1.png");
    lv_obj_set_size(img, 800, 480);

    navigate_to(album_screen);
    show_taskbar(album_screen);
    show_imgs(album_screen);
    show_status_bar(album_screen);
}

// 显示相册
void show_imgs(lv_obj_t * parent)
{
    lv_obj_t * imgCard = lv_obj_create(parent);
    lv_obj_set_size(imgCard, 800, 360);
    lv_obj_set_pos(imgCard, 0, 60);
    lv_obj_set_style_radius(imgCard, 0, 0);
    lv_obj_set_style_border_width(imgCard, 0, 0);
    lv_obj_set_style_radius(imgCard, 0, 0);
    lv_obj_set_style_pad_all(imgCard, 0, 0);

    lv_obj_set_style_bg_opa(imgCard, LV_OPA_0, LV_PART_MAIN);     // 设置背景完全透明
    lv_obj_set_style_border_opa(imgCard, LV_OPA_0, LV_PART_MAIN); // 设置边框透明
    lv_obj_set_style_shadow_opa(imgCard, LV_OPA_0, LV_PART_MAIN); // 设置阴影透明

    // 获取图片总数
    img_num = get_file_node_num(img_head);

    if(img_num <= 0) return; // 无图片时直接返回

    // 计算行数：3列布局，向上取整（如4张图需要2行）
    int row = (img_num + 2) / 3;

    // 遍历所有图片，按行列布局
    for(int i = 0; i < row; i++) {         // 行循环
        for(int j = 0; j < COL_NUM; j++) { // 列循环
            int index = i * COL_NUM + j;   // 当前图片索引
            if(index >= img_num) break;    // 超出图片总数时退出

            // 当前图片位置
            int x = MARGIN + j * (IMG_WIDTH + GAP);
            int y = MARGIN + i * (IMG_HEIGHT + GAP);

            // 创建图片容器
            lv_obj_t * img_container = lv_obj_create(imgCard);
            lv_obj_set_size(img_container, IMG_WIDTH, IMG_HEIGHT);
            lv_obj_set_pos(img_container, x, y);
            lv_obj_set_style_radius(img_container, 0, LV_PART_MAIN);
            lv_obj_set_style_bg_color(img_container, lv_color_hex(0x222222), LV_PART_MAIN);
            lv_obj_set_style_border_width(img_container, 0, LV_PART_MAIN);

            lv_obj_clear_flag(img_container, LV_OBJ_FLAG_SCROLLABLE);     // 不可滚动
            lv_obj_clear_flag(img_container, LV_OBJ_FLAG_PRESS_LOCK);     // 禁用按压锁定
            lv_obj_clear_flag(img_container, LV_OBJ_FLAG_GESTURE_BUBBLE); // 禁用手势冒泡

            // 创建图片对象
            lv_obj_t * img      = lv_img_create(img_container);
            file_node_p currIMG = get_img_path_by_index(img_head, index);
            char img_path[256]  = "S:res/ui/small/";
            strcat(img_path, currIMG->name);
            lv_img_set_src(img, img_path);
            // printf("%s\n", img_path);
            lv_obj_set_style_pad_all(img, 0, 0);
            // lv_img_set_zoom(img, (250 * 256) / 800);

            // 居中显示
            lv_obj_center(img);
            // 3. 添加点击事件 预览大图
            lv_obj_add_event_cb(img_container, show_img_event, LV_EVENT_CLICKED, (void *)(intptr_t)index);
        }
    }
}

void show_img_event(lv_event_t * e)
{
    img_index = (int)(intptr_t)lv_event_get_user_data(e);
    show_big_img();
}

void show_big_img()
{
    if(img_screen != NULL) {
        lv_obj_del(img_screen);
    }
    file_node_p currIMG = get_img_path_by_index(img_head, img_index);
    char start_src[256] = "S:res/img/";
    char img_src[256];
    strcpy(img_src, start_src);
    strcat(img_src, currIMG->name);

    img_screen = lv_obj_create(NULL);
    lv_obj_set_style_radius(img_screen, 0, 0);
    lv_obj_set_style_border_width(img_screen, 0, LV_PART_MAIN);

    lv_obj_t * img_card = lv_obj_create(img_screen);
    lv_obj_set_size(img_card, 800, 480);
    lv_obj_set_style_border_width(img_card, 0, LV_PART_MAIN);
    lv_obj_set_style_bg_color(img_card, lv_color_hex(0x000000), 0);
    // lv_obj_clear_flag(img_card, LV_OBJ_FLAG_SCROLLABLE); // 不可滚动
    lv_obj_set_style_pad_all(img_card, 1, 0);
    lv_obj_set_scroll_dir(img_card, LV_DIR_HOR);
    lv_obj_set_scrollbar_mode(img_card, LV_SCROLLBAR_MODE_OFF); // 隐藏滑动条
    lv_obj_set_style_radius(img_card, 0, 0);

    lv_obj_t * big_img = lv_img_create(img_card);
    lv_img_set_src(big_img, img_src);
    lv_obj_center(big_img); // 居中显示

    lv_obj_add_event_cb(img_card, gesture_event_cb, LV_EVENT_ALL, NULL); // 手势事件监听

    currIMG = get_img_path_by_index(img_head, img_index - 1);
    strcpy(img_src, start_src);
    strcat(img_src, currIMG->name);

    lv_obj_t * left_img = lv_img_create(img_card);
    lv_img_set_src(left_img, img_src);
    lv_obj_set_pos(left_img, -800, 0);

    currIMG = get_img_path_by_index(img_head, img_index + 1);
    strcpy(img_src, start_src);
    strcat(img_src, currIMG->name);

    lv_obj_t * right_img = lv_img_create(img_card);
    lv_img_set_src(right_img, img_src);
    lv_obj_set_pos(right_img, 800, 0);

    // 加载新屏幕
    lv_scr_load(img_screen);

    show_detailed_info(img_screen);
}

void show_detailed_info(lv_obj_t * parent)
{
    lv_obj_t * btn_back = lv_img_create(parent);
    lv_img_set_src(btn_back, "S:res/ui/back_big.png");
    lv_obj_set_pos(btn_back, 10, 0);
    lv_obj_set_size(btn_back, 60, 60);

    lv_obj_add_flag(btn_back, LV_OBJ_FLAG_CLICKABLE); // 可点击
    lv_obj_add_event_cb(btn_back, back_btn_event_cb, LV_EVENT_CLICKED, parent);
    lv_obj_set_style_img_opa(btn_back, LV_OPA_70, LV_STATE_PRESSED); // 添加按下效果
}

// 手势事件回调函数
static void gesture_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_indev_t * indev   = lv_indev_get_act();

    if(code == LV_EVENT_PRESSED) {
        // 记录触摸开始位置和时间
        lv_indev_get_point(indev, &start_point);
        start_time = lv_tick_get();
    } else if(code == LV_EVENT_RELEASED) {
        // 记录触摸结束位置和时间
        lv_indev_get_point(indev, &end_point);
        uint32_t time_diff = lv_tick_elaps(start_time);
        // 获取手势方向
        int direction = get_gesture_direction(start_point, end_point, time_diff);
        if(direction == 3) {
            img_index++;
        } else if(direction == 1) {
            img_index--;
        }
        img_index = (img_index + img_num) % img_num;

        show_big_img();

        // char start_src[256] = "S:/res/img/";
        // char img_src[256];
        // file_node_p currIMG = get_img_path_by_index(img_head, img_index);
        // strcpy(img_src, start_src);
        // strcat(img_src, currIMG->name);
        // lv_img_set_src(big_img, img_src);

        // currIMG = get_img_path_by_index(img_head, img_index - 1);
        // strcpy(img_src, start_src);
        // strcat(img_src, currIMG->name);
        // lv_img_set_src(left_img, img_src);

        // currIMG = get_img_path_by_index(img_head, img_index + 1);
        // strcpy(img_src, start_src);
        // strcat(img_src, currIMG->name);
        // lv_img_set_src(right_img, img_src);
    }
}

// 获取手势方向
static int get_gesture_direction(lv_point_t start, lv_point_t end, uint32_t time_diff)
{
    int dx     = end.x - start.x;
    int dy     = end.y - start.y;
    int abs_dx = abs(dx);
    int abs_dy = abs(dy);

    // 判断是否为有效滑动（距离和速度）
    if(abs_dx < 200 && abs_dy < 200) {
        return -1; // 无效滑动
    }

    // 判断滑动方向
    if(abs_dx > abs_dy) {
        // 水平滑动
        if(dx > 0) {
            return 1; // 右
        } else {
            return 3; // 左
        }
    } else {
        // 垂直滑动
        if(dy > 0) {
            return 2; // 下
        } else {
            return 0; // 上
        }
    }
}

static void back_btn_event_cb(lv_event_t * e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED) {
        lv_obj_t * parent = lv_obj_get_user_data(e);
        lv_obj_del(parent);
        lv_scr_load(page_history[history_index]);
    }
}